<?php
/**
 * zouhao619@gmail.com
 */
class IndexController extends CommonController{
	public function index(){
		$this->display();
	}
	public function setting(){
		if($this->isGet()){
			$this->display();
		}else{
			file_put_contents(CONFIG_PATH.'/config.setting.php','<?php return '.var_export($_POST,true).';');
			$this->success('保存成功');
		}
	}
	public function updatePwd(){
		if($this->isGet()){
			$this->display();
		}else{
			$old_password=md5($_POST['old_password']);
			if(strlen($_POST['password'])<6){
				$rs['status']=5;
				$rs['msg']='新密码必须大于6位';
				exit(json_encode($rs));
			}
			$password=md5($_POST['password']);
			if($old_password!=$_SESSION['admin']['password']){
				$rs['status']=2;
				$rs['msg']='原密码不正确';
				exit(json_encode($rs));
			}
			$admin['id']=$_SESSION['admin']['id'];
			$admin['password']=$password;
			$db=D('Admin');
			if(!$db->create($admin)){
				$rs['status']=3;
				$rs['msg']=$db->getError();
				exit(json_encode($rs));
			}
			if(D('Admin')->update($admin)){
				$_SESSION['admin']['password']=$admin['password'];
				$rs['status']=1;
			}else{
				$rs['status']=4;
				$rs['msg']='更新密码失败,请联系管理员';
			}
			echo json_encode($rs);
		}
	}
	public function main(){
		$this->display();
	}
}